package com.q.tqb.fragment;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.MainActivity;
import com.q.tqb.adapter.BaseFragmentPagerAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.SpinnerInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.NoScrollViewPager;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class GrabFragment extends Fragment {


    @BindView(R.id.tab_grab)
    TabLayout mTabGrab;
    @BindView(R.id.vp_grab)
    ViewPager mVpGrab;
    Unbinder unbinder;

    private BaseFragmentPagerAdapter mFragmentPagerAdapter;

    public GrabFragment() {
    }

    public static GrabFragment newInstance() {
        GrabFragment fragment = new GrabFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_grab, container, false);
        unbinder = ButterKnife.bind(this, view);
        getTabs();
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void setCurrent(int page) {
        if (mVpGrab != null) {
            mVpGrab.setCurrentItem(page);
        }
    }

    private void getTabs() {
        Map<String, String> map = new HashMap<>();

        OkHttpUtil.getInstance().post(getContext(), AppConst.URL_GET_BANK_AREA, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                ArrayList<SpinnerInfo> list = gson.fromJson(result, new TypeToken<ArrayList<SpinnerInfo>>() {
                }.getType());
                addTab(list);
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
            }
        });
    }

    private void addTab(ArrayList<SpinnerInfo> list) {
        ArrayList<String> l = new ArrayList<>();
        for (SpinnerInfo s : list) {
            l.add(s.packet_name);
        }
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            GrabListFragment grabDetailFragment = GrabListFragment.newInstance(Integer.parseInt(list.get(i).level));
            fragments.add(grabDetailFragment);
        }

        mFragmentPagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), getActivity(), fragments, l);
        mVpGrab.setAdapter(mFragmentPagerAdapter);
        mTabGrab.setupWithViewPager(mVpGrab);
        mTabGrab.setTabMode(TabLayout.MODE_FIXED);
        mVpGrab.setOffscreenPageLimit(2);
        mVpGrab.setCurrentItem(0);
    }
}
